package com.nba.common.utils;

import java.awt.Color;
import java.awt.Font;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.alibaba.simpleimage.ImageFormat;
import com.alibaba.simpleimage.ImageRender;
import com.alibaba.simpleimage.ImageWrapper;
import com.alibaba.simpleimage.render.DrawTextItem;
import com.alibaba.simpleimage.render.DrawTextParameter;
import com.alibaba.simpleimage.render.DrawTextRender;
import com.alibaba.simpleimage.render.FixDrawTextItem;
import com.alibaba.simpleimage.render.ReadRender;
import com.alibaba.simpleimage.render.ScaleParameter;
import com.alibaba.simpleimage.render.ScaleRender;
import com.alibaba.simpleimage.render.WatermarkParameter;
import com.alibaba.simpleimage.render.WatermarkRender;
import com.alibaba.simpleimage.render.WriteRender;
import com.alibaba.simpleimage.render.FixDrawTextItem.Position;
import com.alibaba.simpleimage.render.ScaleParameter.Algorithm;

/**
 * 使用阿里巴巴的simpleImage实现图片的剪切，文字图片的彩印等功能
 * @author  2015-12-28 
 *
 */
public class SimpleImgUtil {
	
	/**
     * 执行裁剪
     * @param in 要裁剪的图片
     * @param out 后输出的文件
     * @param w 宽
     * @param h 高
     * @param algorithm 缩略算法
     * @throws Exception
     */
    public static void doScale(InputStream in,OutputStream out,int w,int h, Algorithm algorithm) throws Exception {
        InputStream input = null;
        ImageRender sr = null;
        try {
            sr = new ScaleRender(in, getParam(w,h,algorithm));
            write(sr,out);
        } finally {
            if (sr != null) {
                sr.dispose();
            }
            IOUtils.closeQuietly(input);
        }
    }
    
    /**
     * 执行图片水印
     * @param inbg 要水印的背景图片
     * @param inwm 水印图片
     * @param out 水印后输出的文件
     * @param alpha 水印透明度 0-1
     * @param x 水印坐标
     * @param y
     * @throws Exception
     */
    public static void doWatermark(InputStream inbg,InputStream inwm,OutputStream out, float alpha, int x, int y) throws Exception {
        ImageRender sr = null;
        try {
            ImageRender rr = new ReadRender(inbg, true);
            sr = new WatermarkRender(rr, createParam(inwm, alpha, x, y));
            write(sr,out);
        } finally {
            if (sr != null) {
                sr.dispose();
            }
            IOUtils.closeQuietly(inbg);
        }
    }
    
    /**
     * 执行文字水印
     * @param in
     * @param out
     * @param text
     * @param fontColor
     * @param fontShadowColor
     * @param font
     * @param minFontSize
     * @param textWidthPercent
     * @param position
     * @throws Exception
     */
    public static void doDrawText(InputStream in,OutputStream out,String text, Color fontColor, Color fontShadowColor, Font font, int minFontSize,
            float textWidthPercent,Position position) throws Exception {
    	DrawTextItem item = new FixDrawTextItem(text, fontColor, fontShadowColor, 
    			font, minFontSize, position, textWidthPercent);
        doDrawText(in,out,item);
    }
    
    /**
     * 执行文字水印
     * @param in
     * @param out
     * @param items
     * @throws Exception
     */
    public static void doDrawText(InputStream in,OutputStream out, DrawTextItem... items) throws Exception {
        ImageRender fr = null;
        try {
            ImageRender rr = new ReadRender(in);
            DrawTextParameter dp = new DrawTextParameter();
            if (items != null) {
                for (DrawTextItem itm : items) {
                    dp.addTextInfo(itm);
                }
            }
            DrawTextRender dtr = new DrawTextRender(rr, dp);
            fr = new WriteRender(dtr, out);
            fr.render();
        } finally {
            IOUtils.closeQuietly(in);
            if (fr != null) {
                fr.dispose();
            }
        }
    }
    
    public static WatermarkParameter createParam(InputStream  in, float alpha, int x, int y) throws Exception {
        ImageRender rr = null;
        ImageWrapper imageWrapper = null;
        try {
            rr = new ReadRender(in, false);
            imageWrapper = rr.render();
        } finally {
            if (rr != null) {
                rr.dispose();
            }
            IOUtils.closeQuietly(in);
        }
        WatermarkParameter param = new WatermarkParameter(imageWrapper, alpha, x, y);
        return param;
    }
    
    public static ScaleParameter getParam(int w,int h, Algorithm algorithm) {
        ScaleParameter param = new ScaleParameter(w, h,Algorithm.AUTO);

        return param;
    }
    
    public static void write(ImageRender sr,OutputStream out) throws Exception {
        ImageRender wr = null;
        try {
            wr = new WriteRender(sr, out, ImageFormat.JPEG);
            wr.render();
        } finally {
            if (wr != null) {
                wr.dispose();
            }
            IOUtils.closeQuietly(out);
        }
    }
    
    /**
     * 根据水印位置获取X,Y坐标
     * @param pos 水印位置
     * @param width 目标文件宽度
     * @param height 目标文件高度
     * @param width_biao 水印文件宽度
     * @param height_biao 水印文件高度
     * @return Map<Integer,Integer>
     */
    public static Map<String,Integer> getXAndY(int pos,int width,int height,int width_biao,int height_biao) {
    	int x = 0;
    	int y = 0;
    	Map<String,Integer> map = new HashMap<String,Integer>();
    	switch(pos) {
			case 2:
				x = (width - width_biao) / 2;
				y = 0;
				break;
	        case 3:
	        	x = width - width_biao;
				y = 0;
				break;
	        case 4:
	        	x = width - width_biao;
				y = (height - height_biao) / 2;
				break;
	        case 5:
	        	x = width - width_biao;
				y = height - height_biao;
				break;
	        case 6:
	        	x = (width - width_biao) / 2;
				y = height - height_biao;
				break;
	        case 7:
	        	x = 0;
				y = height - height_biao;
				break;
	        case 8:
	        	x = 0;
				y = (height - height_biao) / 2;
				break;
	        case 9:
	        	x = (width - width_biao) / 2;
				y = (height - height_biao) / 2;
	        	break;
	        default:
	        	x = 0;
	        	y = 0;
	        	break;
    	}
    	map.put("x", x);
    	map.put("y", y);
    	return map;
    }

}
